/**
 * Created on 2009-4-17 下午02:53:26 <br>
 * at Tianhe Guangzhou
 */
package com.rich.component.transaction.demo.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.rich.component.transaction.demo.dao.TransactionTestDao;
import com.rich.component.transaction.demo.domain.DemoObject;
import com.rich.component.transaction.demo.service.TransactionTestService;

/**
 * . <br>
 * 
 * @author xdan
 */
public class TransactionTestServiceImpl implements TransactionTestService {
	TransactionTestDao transactionTestDao;

	public TransactionTestDao getTransactionTestDao() {
		return transactionTestDao;
	}

	public void setTransactionTestDao(TransactionTestDao transactionTestDao) {
		this.transactionTestDao = transactionTestDao;
	}

	public DemoObject getOneDemo() {
		List<DemoObject> list = transactionTestDao.getDemoList(null);
		return list.size() > 0 ? (DemoObject) list.get(0) : null;
	}

	private void throwException() {
		System.out.println("抛异常！");
		throw new RuntimeException("测试事务---抛运行时异常!");
	}

	@Transactional
	public DemoObject findDemoObject(Long id) {
		System.out.println("进入 findDemoObject");

		System.out.println("退出 findDemoObject");
		return getOneDemo();
	}

	@Transactional
	public void updateDemoObject(DemoObject demo) {
		// TODO Auto-generated method stub
		System.out.println("###进入 updateDemoObject");
		// demo.setContent(demo.getContent() + "add test");
		// transactionTestDao.updateDemoObject(demo);
		transactionTestDao.insertDemoObject(demo);
		// throwException();
		System.out.println("###退出  updateDemoObject");
	}

	 @Transactional
	public void insertDemoObject(DemoObject demo) {
		// TODO Auto-generated method stub
		System.out.println("###进入 insertDemoObject");

		transactionTestDao.insertDemoObject(demo);

		// throwException();
		System.out.println("###退出  insertDemoObject");

	}

}
